<?php
class Codegen_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }
	public function get_all($table_name){
        return $this->db->get($table_name)->result_array();
    }
    function get_by($table, $key, $val = FALSE, $orwhere = FALSE, $single = FALSE){
		if(!is_array($key)){
			$this->db->where(htmlentities($key),htmlentities($val));
		}else{
			$key			= array_map('htmlentities',$key);
			$where_method	= $orwhere	== TRUE ? "or_where" : "where";
			$this->db->$where_method($key);
		}
		
		$single == FALSE || $this->db->limit(1);
		$method = $single ? 'row_array' : 'result_array';
		return $this->db->get($table)->$method();
	}
    function get($table,$fields,$where='',$perpage=0,$start=0,$one=false,$array='array'){
        
        $this->db->select($fields);
        $this->db->from($table);
        $this->db->limit($perpage,$start);
        if($where){
        $this->db->where($where);
        }
        
        $query = $this->db->get();
        
        $result =  !$one  ? $query->result($array) : $query->row() ;
        return $result;
    }
	function getFG($table_name,$key=FALSE){ //Zulmi Adi Rizki 20 Juli 2013
		if($key == TRUE){
			$key = array_map('htmlentities', $key); 
			$this->db->where($key);
		}
		$this->db->select("*");
		$this->flexigrid->build_query();
		$return['records'] = $this->db->get($table_name);
		
		$this->db->select("*");
		$this->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results($table_name);
		
		return $return;
	}
    function get_joinFG($table_name,$data,$key=FALSE,$column=FALSE){ //Zulmi Adi Rizki 23 Juli 2013
		
		if($key == TRUE){
			if(!is_array($key)){
				$this->db->where($key);
			} else {
				$key = array_map('htmlentities', $key);
				$this->db->where($key);
			}
		}
		$this->db->select($column == FALSE ? "*" : $column)->from($table_name);
		foreach($data as $row){
			$this->db->join($row["join_table"],$row["table"].".".$row["join_key"]."=".$row["join_table"].".".$row["join_key"]);
		}
		$this->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select($column == FALSE ? "*" : $column)->from($table_name);
		foreach($data as $row){
			$this->db->join($row["join_table"],$row["table"].".".$row["join_key"]."=".$row["join_table"].".".$row["join_key"]);
		}
		$this->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
    function add($table,$data){
        $this->db->insert($table, $data);         
        if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
		
		return FALSE;       
    }
    
    function edit($table,$data,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        $this->db->update($table, $data);

        if ($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		
		return FALSE;       
    }
    
    function delete($table,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        $this->db->delete($table);
        if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
		
		return FALSE;        
    }   
	
	function count($table){
		return $this->db->count_all($table);
	}
}